package com.pdsu.xpf.graduation01.dao;

import com.pdsu.xpf.graduation01.domain.Commodity;
import com.pdsu.xpf.graduation01.domain.CommodityType;
import com.pdsu.xpf.graduation01.domain.Type;
import com.pdsu.xpf.graduation01.dto.CommodityLikeDTO;
import com.pdsu.xpf.graduation01.vo.CommodityVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ICommodityDao {

    @Select("select * from commodity where state = 1 ")
    List<Commodity> findAllRecommendCommodity();
    @Select("select * from commodity where cid = #{cid} ")
    Commodity getCommodityByCID(int cid);

    @Select("select * from commodity where ctid = #{ctid}")
    public List<Commodity> getCommodityByCTID(int ctid);

    @Select("select co.cid,co.uid ,co.cname, co.cmessage ,co.cnumber, co.image ,co.price ,co.sell_desc ,co.username  from commodity as co,user_like as ul WHERE ul.uid = #{uid} and ul.cid = co.cid ")
    List<CommodityLikeDTO> getLikeCommodity(int uid);

    @Insert("insert into user_like(uid,cid) values ( #{uid},#{cid} ) ")
    int addLikeCommodity(int uid,int cid);

    @Delete("delete from user_like where uid = #{uid} and cid = #{cid}")
    int delLikeCommodity(int uid, int cid);

    @Select("select * from user_like where uid = #{uid} and cid = #{cid}")
    Integer commodityIsLike(int uid, int cid);

    @Select("select * from user_trolley where uid = #{uid} and cid = #{cid}")
    Integer carIsHaveCommodity(int uid, int cid);

    @Insert("insert into user_trolley(uid,cid,ucnumber) values(#{uid},#{cid},1) ")
    Integer addCommodityToCar(int uid, int cid);

    @Select("select * from commodity where cname like #{key}")
    List<Commodity> findCommodityByKey(String key);

    @Insert("insert into commodity(uid,ctid,price,cnumber,cname,cmessage,username,image,sell_desc,date) values(#{uid},#{ctid},#{cprice},#{cnumber},#{cname},#{cmessage},#{username},#{cimage},'一件起卖',#{date})")
    Integer insertACommodity(int uid,int ctid,Double cprice,int cnumber,String cname,String cmessage,String username,String cimage ,String date);

    @Select("Select * from type")
    List<Type> getAllType();

    @Select("select * from commodity_type where tid = #{tid}")
    List<CommodityType> getCommodityTypeByTID(int tid);

    @Select("select * from commodity")
    List<Commodity> findAllCommodity();

    @Select("select * from commodity as co ,commodity_type as ct ,type as ty where co.ctid = ct.ctid and ct.tid =ty.tid;")
    List<CommodityVO> findAllCommodityVO();

    @Delete("delete from commodity where cid = #{cid} ")
    Integer delCommodityByCID(int cid);

    @Select("select * from commodity as co ,commodity_type as ct ,type as ty where co.ctid = ct.ctid and ct.tid =ty.tid and co.state = 0")
    List<CommodityVO> findAllCheckCommodityVO();

    @Update("update commodity set state = 1 where cid = #{cid}")
    Integer upCommodityByCID(int cid);

    @Select("select * from commodity as co ,commodity_type as ct ,type as ty where co.ctid = ct.ctid and ct.tid =ty.tid  and co.cname like #{key}")
    List<CommodityVO> searchCommodityByKey(String key);

    @Select("select ctid from commodity_type where type = #{typename} ")
    int getCTIDbyTYPENAME(String typename);

    @Update("update commodity set price = #{price} , ctid = #{ctid} where cid = #{cid}")
    int updateCommodityByCID(int cid, double price, int ctid);
}
